package com.matt.controller;

import com.matt.commons.model.StatusResponse;
import com.matt.service.UploadService;

import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.matt.foundation.model.ErrorCode.FILE_MAST_NOT_NULL;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/7/26
 * *******************************
 */
@RestController
@RequestMapping(value = "/system")
public class SystemController {

    @Autowired
    private UploadService uploadService;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;




    @ApiOperation(value = "文件上传",httpMethod = "POST")
    @RequestMapping(value = "/upload" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse upload(MultipartFile file) {
        Map result = new HashMap();
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MAST_NOT_NULL,"文件不能为空");
        String url = uploadService.uploadFile(file);
        String name =  url.substring(url.lastIndexOf("/")+1,url.length());
        result.put("name",name);
        result.put("url",url);
        return StatusResponse.success(result);
    }

    @ApiOperation(value = "文件上传带文件名",httpMethod = "POST")
    @RequestMapping(value = "/upload1" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse upload1(MultipartFile file) {
        Map result = new HashMap();
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MAST_NOT_NULL,"文件不能为空");
        String url = uploadService.uploadFile(file);
        String name =  url.substring(url.lastIndexOf("/")+1,url.length());
        result.put("name",name);
        result.put("url",url);
        String originalName = file.getOriginalFilename();

        result.put("originalName",originalName);
        return StatusResponse.success(result);
    }



}
